package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.TargetTypeDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.TargetType;
import com.bestv.search.common.model.uid.UID;
import com.bestv.search.common.service.TargetTypeManager;
import com.bestv.search.common.util.CommandException;

import java.util.List;

public class TargetTypeManagerImpl implements TargetTypeManager {

    private TargetTypeDao targetTypeDao;

    public TargetTypeDao getTargetTypeDao() {
        return targetTypeDao;
    }

    public void setTargetTypeDao(TargetTypeDao targetTypeDao) {
        this.targetTypeDao = targetTypeDao;
    }

    public TargetType getTargetById(DataSourceKey key, Long id) {
        return targetTypeDao.getTargetById(id);
    }

    public TargetType getTargetByName(DataSourceKey key, String name) {
        return targetTypeDao.getTargetByName(name);
    }

    public List<TargetType> getTargetTypes(DataSourceKey key) {
        return targetTypeDao.getTargetTypes();
    }

    public void deleteTargetType(DataSourceKey key, Long id) throws CommandException {
        targetTypeDao.remove(id);
    }

    public TargetType saveTargetType(DataSourceKey key, TargetType targetType) {
        if (targetType.getId() == 0L) {
            targetType.setId(new Long(UID.getUID(targetType)));
        }
        return targetTypeDao.saveOrUpdate(targetType);
    }
}
